﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using TaobaoDistrSales.Common;
using TaobaoDistrSales.Model;
using System.Threading;

namespace TaobaoDistrSales
{
    public partial class MainOperationForm : DockContent
    {
        #region Ctor

        public MainOperationForm()
        {
            InitializeComponent();
        }

        #endregion

        #region Properties

        public int TotalDashBoardCount
        {
            get;
            set;
        }

        private List<int> ProcessShopIDList
        {
            get;
            set;
        }
         
        #endregion

        #region Load

        private void MainOperationForm_Load(object sender, EventArgs e)
        { 
            ProcessShopIDList = new List<int>();

            using (taobaodbEntities entities = ResourceManager.LoadEntity())
            {
                OptionsConfig config = entities.OptionsConfig.FirstOrDefault();
                TotalDashBoardCount = config != null ? config.displaypanelnum : 1;
                MainSplitContainer.Panel2Collapsed = TotalDashBoardCount <= 1;

                // TODO: I only consider the only one panel in the dashboard.    
                //var shop = ResourceManager.Current.Entities.Shop.First();
                var shop = entities.Shop.FirstOrDefault();
                if (shop == null)
                {
                    MessageBox.Show("数据库里没有店铺信息，请先输入店铺信息后再运行程序。");
                    return;
                }

                TaobaoFeiXiaoProcessor processor = new TaobaoFeiXiaoProcessor(shop.ShopId);
                ProcessShopIDList.Add(shop.ShopId);
                processor.SwitchShop = sh =>
                {
                    var shopid = sh.ShopId;
                    try
                    {
                        using (taobaodbEntities entities0 = ResourceManager.LoadEntity())
                        {
                            var shops = (from s in entities0.Shop
                                        orderby s.ShopId 
                                        select s.ShopId ).ToList();
                            var s1 = shops.FirstOrDefault(sp =>
                            {
                                return !ProcessShopIDList.Contains(sp);
                            });
                            if (s1 == 0)
                            {
                                if (shops.Count > 0)
                                    shopid = shops[0];
                                else
                                    shopid = sh.ShopId;
                                ProcessShopIDList.Clear();
                            }
                            else
                            {
                                shopid = s1; 
                            }
                            ProcessShopIDList.Add(shopid);
                            return shopid;
                        }
                    }
                    catch { }
                    return shopid;
                };
                this.LeftTopDashBoardControl.TaobaoProcessor = processor;
            }          
               
        }
        
        #endregion

        #region Processor Events
         
        #endregion
         
         
    }
}
